<template>
  <div class="pwd-container">
    <el-form autoComplete="on" :model="changePwdForm" :rules="changeRules" ref="changePwdForm" label-position="left" label-width="0px" class="card-box pwd-form">
      <h3 class="title">
        修改密码
      </h3>

      <!-- <el-alert class="tips" v-if="errorMsg.length>0" :title="errorMsg" type="error" show-icon :closable="false">
      </el-alert> -->

      <el-form-item prop="oldpassword">
        <span class="svg-container">
          <svg-icon icon-class="password"></svg-icon>
        </span>
        <el-input name="oldpassword" :type="pwdType" @keyup.enter.native="handleChange" v-model="changePwdForm.oldpassword" placeholder="旧密码"></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"></svg-icon>
        </span>
        <el-input name="password" :type="pwdType" @keyup.enter.native="handleChange" v-model="changePwdForm.password" placeholder="新密码"></el-input>
      </el-form-item>

      <el-form-item prop="repassword">
        <span class="svg-container">
          <svg-icon icon-class="password"></svg-icon>
        </span>
        <el-input name="repassword" :type="pwdType" @keyup.enter.native="handleChange" v-model="changePwdForm.repassword" placeholder="确认新密码"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" style="width:100%;" :loading="loading" @click.native.prevent="handleChange">
          提交
        </el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
  import { Message } from 'element-ui'

  export default {
    name: 'changepwd',
    data() {
      return {
        loading: false,
        pwdType: 'password',
        errorMsg: '',
        changePwdForm: {
          oldpassword: '',
          password: '',
          repassword: '',
        },
        changeRules: {
          oldpassword: [
            {
              type: 'string',
              required: true,
              trigger: 'blur',
              message: '请输入旧密码',
            },
          ],
          password: [
            {
              type: 'string',
              required: true,
              trigger: 'blur',
              message: '请输入新密码',
            },
            {
              min: 6,
              max: 10,
              trigger: 'change',
              message: '长度在6-10位',
            },
            {
              pattern: /^[a-zA-Z0-9]+$/,
              trigger: 'blur',
              message: '只能输入英文字符和数字',
            },
          ],
          repassword: [
            {
              type: 'string',
              required: true,
              trigger: 'blur',
              message: '请确认密码',
            },
            {
              min: 6,
              max: 10,
              trigger: 'change',
              message: '长度在6-10位',
            },
            {
              // 自定义验证
              validator: (rule, value, callback) => {
                if (value !== this.changePwdForm.password) {
                  callback(new Error('两次输入的密码不一致!'))
                } else {
                  callback()
                }
              },
              trigger: 'blur',
            },
          ],
        },
      }
    },

    methods: {
      showPwd() {
        if (this.pwdType === 'password') {
          this.pwdType = ''
        } else {
          this.pwdType = 'password'
        }
      },
      checkConfirm(param) {
        // 验证新密码是否一致
      },
      handleChange() {
        this.$refs.changePwdForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store
              .dispatch('MemberChangePwd', { ChangePwdForm: this.changePwdForm })
              .then(response => {
                this.loading = false
                this.errorMsg = ''
                if (response === true) {
                  // 修改成功
                  Message.success('修改成功,请重新登录')
                  // 清空状态,重新登录
                  this.$store
                    .dispatch('FedLogOut')
                    .then(this.$router.push({ path: '/login' }))
                } else {
                  // 修改失败
                  this.errorMsg = Object.values(response)[0].toString() // 取第一个错误信息
                }
              })
              .catch(() => {
                this.loading = false
              })
          } else {
            console.log('validate fail')
            return false
          }
        })
      },
    },
  }
</script>

<style rel="stylesheet/scss" lang="scss">
  $bg: white;
  $dark_gray: #889aa4;
  $light_gray: #eee;
  $light_blue: #b3d8ff;
  $dark_blue: #ecf5ff;

  .pwd-container {
    // position: fixed;
    height: 100%;
    width: 100%;
    background-color: $bg;
    input:-webkit-autofill {
      -webkit-box-shadow: 0 0 0px 1000px white inset !important;
      -webkit-text-fill-color: black !important;
    }
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: black;
      height: 47px;
    }
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
    }
    .tips {
      font-size: 14px;
      margin-bottom: 10px;
    }
    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .title {
      font-size: 16px;
      font-weight: 400;
      color: black;
      margin: 0px auto 20px auto;
      text-align: center;
      font-weight: bold;
    }
    .pwd-form {
      // position: absolute;
      // left: 0;
      // right: 0;
      width: 400px;
      padding: 35px 35px 15px 35px;
    }
    .el-form-item {
      border: 1px solid $light_blue;
      // background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
    .verify-code {
      width: 40%;
    }
    .show-code {
      position: absolute;
      right: 10px;
      width: 40%;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
    .thirdparty-button {
      position: absolute;
      right: 35px;
      bottom: 28px;
    }
  }
</style>
